﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Linq.Expressions;

namespace Text.Helper
{
    public static class PropertyHelper
    {
        public static string GetDisplayName<T, TReturn>(this T val, Expression<Func<T, TReturn>> express)
        {
            var memberExpress = express.Body as MemberExpression;
            var disp = (DisplayAttribute) memberExpress.Member.GetCustomAttributes(typeof(DisplayAttribute), true)
                .FirstOrDefault();

            if (disp != null)
                return disp.Name;
            return memberExpress.Member.Name;
        }
    }
}